#!/usr/bin/env python
#coding=utf-8
from common.web import Web
from common.tools import Tools


class YYWeb(Web):      
    def __init__(self, proxy='', logfile='', interval_time=180):    
        self.host = 'www.yy365.com'
        Web.__init__(self, proxy=proxy, logfile=logfile, interval_time=interval_time) 
 
    def login(self, email, pwd):          
        referer = 'http://www.yy365.com/'
        url = 'http://www.yy365.com/login.do'
        email = Tools.urlEncode(email)
        pwd = Tools.urlEncode(pwd)
        data = 'ytsnsEmail=%s&password=%s&Submit2=%%E7%%99%%BB+%%E5%%BD%%95' % (email, pwd)
        return self.fetchData(referer, url, data) 
    
    def listFriends(self): 
        #获得所有好友列表  
        referer = 'http://www.yy365.com/friend.do'
        url = 'http://www.yy365.com/friend!friendByLasttime.do?sign=downlevel&s=0'
        return self.fetchData(referer, url) 
    
    ###############blog################    
    def listMyBlog(self): 
        #查看我的博客列表，为了得到未分类的id，仅获一次放到数据库里 
        #todo 解析分类号        
        referer = 'http://www.yy365.com/blog!listAllBlogs.do'
        url = 'http://www.yy365.com/blog!listMySelfBlogs.do'
        return self.fetchData(referer, url)  
        
    def postBlog(self, title, content, categoryid): 
        #发表博客    openType=1所有好友可见；0全站可见     
        referer = 'http://www.yy365.com/blog!addBlog.do'
        url = 'http://www.yy365.com/blog!saveBlog.do'
        title = Tools.urlEncode(title)
        content = Tools.urlEncode(content)
        data = '_isAjaxRequest=true&blogTitle=%s&blogContent=%s&openType=1&ownerBlog=checkbox&share=0&label1=&label2=&label3=&categoryIds=&categoryIds1=&categoryid=%s' % (title, content, categoryid)
        return self.fetchData(referer, url, data)      
    
    def listBlogs(self, id): 
        #获得好友今天最新博客id,title        
        referer = 'http://www.yy365.com/home!other.do?id=%s' % id
        url = 'http://www.yy365.com/home!diary.do?id=%s' % id
        return self.fetchData(referer, url)  
     
    def saveBlogComment(self, blogid, bloggerid, comment, blogtitle): 
        #给好友博客留言        
        referer = 'http://www.yy365.com/blog!viewBlog.do?blogId=%s&userId=%s&n=1' % (blogid, bloggerid)
        url = 'http://www.yy365.com/blog!saveBlogComment.do'
        comment = Tools.urlEncode(comment)
        blogtitle = Tools.urlEncode(blogtitle)
        data = '_isAjaxRequest=true&blogId=%s&commentContent=%s&bloggerId=%s&isReplay=false&blogTitle=%s' % (blogid, comment, bloggerid, blogtitle)
        return self.fetchData(referer, url, data)  
    
    ############twitter######################### 
    def postMiniBlog(self, content): 
        #发表迷你博客         
        referer = 'http://www.yy365.com/miniblog!listMy.do'
        atime = Tools.makeYYTime()
        url = 'http://www.yy365.com/miniblog!saveMiniBlog.do?t=%s' % atime
        content = Tools.urlEncode(content)
        data = '_isAjaxRequest=true&content=%s&openType=0&push=0&categories=' % content
        return self.fetchData(referer, url, data) 

    def listMiniBlogs(self, id): 
        #获得好友今天最新mini博客id        
        referer = 'http://www.yy365.com/home!other.do?id=%s' % id
        url = 'http://www.yy365.com/home!mini.do?id=%s' % id
        return self.fetchData(referer, url)  
                                  
    def saveMiniBlogComment(self, mainId, userId, content): 
        #给好友迷你博客留言        
        referer = 'http://www.yy365.com/miniblog!showMiniBlog.do?id=%s&userId=%s' % (mainId, userId)
        atime = Tools.makeYYTime()
        url = 'http://www.yy365.com/miniblog!saveComment.do?mainId=%s&userId=%s&%s' % (mainId, userId, atime)
        content = Tools.urlEncode(content)
        data = '_isAjaxRequest=true&content=%s&toUserId=0&commentId=0&toUserName=&toLevel=0' % content
        return self.fetchData(referer, url, data)      
#    def getMiniBlog(self, id, userid): 
#        #查看迷你博客页面  
#        #id博客，userid博主      
#        referer = 'http://www.yy365.com/home.do'
#        url = 'http://www.yy365.com/miniblog!showMiniBlog.do?id=%s&userId=%s' % (id, userid)
#        return self.fetchData(referer, url)  
            
    def daka(self): 
        #打卡        
        referer = 'http://www.yy365.com/home.do'
        url = 'http://www.yy365.com/login!loginintegral.do'
        data = '_isAjaxRequest=true'
        return self.fetchData(referer, url, data) 
    
    #######自动注册##############################
    def invitelist(self): 
        #获取邀请好友地址
        referer = 'http://www.yy365.com/home.do'
        url = 'http://www.yy365.com/invite!invitelist.do?point=-1'
        return self.fetchData(referer, url)
      
    
    def reg(self, id, sid, code, info): 
        #发送注册信息
        #http://www.yy365.com/invite!from.do?id=211m1o221m20ba840a331b&&code=1281758424
        referer = 'http://www.yy365.com/invite!from.do?id=%s&&code=%s' % (id, code)
        url = 'http://www.yy365.com/reg!Reg.do'
        #_isAjaxRequest=true&name=%E8%B5%B5%E7%90%A8&email=689488%40qq.com&password=19840624&city=1830&sid=502604&code=1281758424&fr=&validateCode=%E5%A5%8B%E5%87%89&sex=2
        
        email = Tools.urlEncode(info['email']) 
        pwd = info['pwd'] #密码要不要编码？？？？
        name = Tools.urlEncode(info['name'])
        sex = info['sex'] 
        city = info['city'] 
        validateCode = Tools.urlEncode(info['validateCode'])
        
        data = '_isAjaxRequest=true&name=%s&email=%s&password=%s&city=%s&sid=%s&code=%s&fr=&validateCode=%s&sex=%s' % (name, email, pwd, city, sid, code, validateCode, sex)
        return self.fetchData(referer, url, data)  
    
    def existsEmail(self, id, code, email): 
        #验证注册邮箱是否存在
        referer = 'http://www.yy365.com/invite!from.do?id=%s&&code=%s' % (id, code)
        url = 'http://www.yy365.com/reg!existsEmail.do'
        data = '_isAjaxRequest=true&email=%s' % email
        return self.fetchData(referer, url, data)    
    
    def retrieveImg(self, id, code): 
        #获取验证码图片地址
        referer = 'http://www.yy365.com/invite!from.do?id=%s&&code=%s' % (id, code)
        url = 'http://www.yy365.com/reg!retrieveImg.do'
        return self.fetchData(referer, url)   

    def getCodeImg(self, url, id, code):
        #获取验证码图片
        referer = 'http://www.yy365.com/invite!from.do?id=%s&&code=%s' % (id, code)
        return self.fetchData(referer, url)  
    
#    def getMain(self):          
#        referer = 'http://www.yy365.com/login.do'
#        url = 'http://www.yy365.com/'
#        return self.fetchData(referer, url) 
    
     
         

    
#    def getBlog(self, blogid, userid): 
#        #查看博客页面  
#        #id博客，userid博主      
#        referer = 'http://www.yy365.com/home.do'
#        url = 'http://www.yy365.com/blog!viewShareBlog.do?blogId=%s&userId=%s' % (blogid, userid)
#        return self.fetchData(referer, url)             
